//
//  SEGIAnalyticsEventDBManager.m
//  test
//
//  Created by 刘山 on 2017/12/3.
//  Copyright © 2017年 刘山. All rights reserved.
//

#import "SEGIAnalyticsEventDBManager.h"

static NSString* SEGIAnalyticsEventDBName = @"SEGEventDBName";//数据库名
static NSString* SEGIAnalyticsEventTableName = @"SEGEventTableName";//数据表名

@implementation SEGIAnalyticsEventDBManager
/**
 * 创建页面统计数据表
 * @author Sam
 */
+(void)segPageCreateTable{
    
    if ([self isTableOK:SEGIAnalyticsEventTableName DB:SEGIAnalyticsEventDBName]) {
        return;
    }
    NSArray *allKeys = [self segAllKeysFromModel:[SEGIAnalyticsLogEventModel class]];
    
    NSMutableString *sql = [NSMutableString string];
    [sql appendString:[NSString stringWithFormat:@"CREATE TABLE %@ (",SEGIAnalyticsEventTableName]];
    [allKeys enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *content = (idx == allKeys.count - 1) ? [NSString stringWithFormat:@" %@ TEXT ",obj] : [NSString stringWithFormat:@" %@ TEXT ,",obj];
        [sql appendString:content];
    }];
    [sql appendString:@");"];
    
    [self segCreateTableWithDBName:SEGIAnalyticsEventDBName WithTable:sql];
}

/**
 * 把统计数据保存的本地数据库
 * @author Sam
 * @param data  事件
 */
+(BOOL)segEventInsertWithData:(NSDictionary*)data{
    return [self segInsertWithDictionary:data DBName:SEGIAnalyticsEventDBName WithTable:SEGIAnalyticsEventTableName];
}

/**
 * 所有数据库中所有事件数据
 * @author Sam
 */
+(NSArray*)segEventQuryAll{
    NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM %@ ;",SEGIAnalyticsEventTableName];
    NSArray *data = [self segExecuteQueryWithDBName:SEGIAnalyticsEventDBName WithSQL:querySql OfValues:nil];
    
    if (data && data.count > 0) {
        return data;
    }
    return nil;
}

/**
 * 删除所有数据
 * @author Sam
 */
+(BOOL)segDeleteAll{
    
    NSString *delSql = [NSString stringWithFormat:@"DELETE FROM %@;",SEGIAnalyticsEventTableName];
    return [self segExecuteDB:SEGIAnalyticsEventDBName WithSQL:delSql OfValues:nil];
}
@end
